package com.whsxt.controller;

import com.whsxt.domain.OrderSettlement;
import com.whsxt.service.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-03-30 11:25
 * @Description:
 */
@RestController
@RequestMapping
@Api(tags = "处理支付的接口")
@Slf4j
public class PayController {


    @Autowired
    private PayService payService;


    @PostMapping("/p/order/pay")
    @ApiOperation(value = "生成支付二维码")
    public ResponseEntity<String> createPayCode(@RequestBody OrderSettlement orderSettlement) {
        String code = payService.pay(orderSettlement);
        return ResponseEntity.ok(code);
    }


    /**
     * 支付宝不能直接调用这个接口，因为支付宝没有token
     * @param map
     * @return
     */
    @PostMapping("/payNotify")
    @ApiOperation(value = "处理支付宝回调的接口")
    public ResponseEntity<String> payNotify(@RequestParam Map<String, String> map) {
        String status = map.get("trade_status");
        if ("TRADE_SUCCESS".equals(status)) {
            //说明支付成功
            //验证是否是支付宝回调
            Boolean flag = payService.checkRes2(map);
            //如果是支付宝回调的，我们就去修改数据库的订单状态
            if (flag) {
                String orderNum = map.get("out_trade_no");
                payService.paySuccessChangeStatus(orderNum);
            } else {
                log.info("不是支付宝调用的");
            }
        }
        return ResponseEntity.ok("ok");
    }


}
